package com.caigou.modules.tools.ui

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.caigou.comm.base.container.BaseFragment
import com.caigou.comm.base.ext.dp2px
import com.caigou.comm.base.ext.screenCapture
import com.caigou.comm.base.ext.showToast
import com.caigou.comm.base.utils.MediaUtils
import com.caigou.modules.tools.R
import com.caigou.modules.tools.adapter.InviteBannerAdapter
import com.caigou.modules.tools.databinding.FragmentInviteBinding
import com.caigou.modules.tools.widget.InviteTipsDialog
import com.zhpan.bannerview.constants.PageStyle
import com.zhpan.indicator.enums.IndicatorSlideMode
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author #Suyghur.
 * Created on 2021/10/14
 */
@AndroidEntryPoint
class InviteFragment : BaseFragment() {

    private var _binding: FragmentInviteBinding? = null
    private val binding get() = _binding!!

    private val mAdapter: InviteBannerAdapter by lazy {
        InviteBannerAdapter()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = FragmentInviteBinding.inflate(inflater, container, false)
        return _binding?.root
    }

    override fun initView() {
        val list = mutableListOf(
            R.drawable.img_tools_invite1,
            R.drawable.img_tools_invite2,
            R.drawable.img_tools_invite3
        )
        binding.inviteBvpBanner.run {
            setLifecycleRegistry(lifecycle)
            setAdapter(mAdapter)
            setIndicatorSlideMode(IndicatorSlideMode.SCALE)
            setIndicatorSliderColor(
                resources.getColor(R.color.caigou_orange),
                resources.getColor(R.color.caigou_theme_color)
            )
            setInterval(5000)
                .setPageMargin(dp2px(requireActivity(), 20f))
                .setScrollDuration(800)
                .setRevealWidth(dp2px(requireActivity(), 20f), dp2px(requireActivity(), 20f))
                .setPageStyle(PageStyle.MULTI_PAGE_OVERLAP)
                .create(list)
        }

        binding.inviteIvHelp.setOnClickListener {
            val dialog = InviteTipsDialog(requireActivity())
            dialog.show()
        }

        binding.inviteLlWechat.setOnClickListener {
            val pos = binding.inviteBvpBanner.currentItem
            val bitmap = mAdapter.views[pos].screenCapture()
            MediaUtils.saveBitmap(requireActivity(), bitmap)
            showToast("截图已保存到相册")
        }

        binding.inviteLlQq.setOnClickListener {
            val pos = binding.inviteBvpBanner.currentItem
            val bitmap = mAdapter.views[pos].screenCapture()
            MediaUtils.saveBitmap(requireActivity(), bitmap)
            showToast("截图已保存到相册")
        }

        binding.inviteTvBack.setOnClickListener {
            (requireActivity() as ToolsActivity).onBackPressed()
        }
    }

    override fun lazyLoad() {
    }

    override fun onBack() {
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}